/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.iquest.explorer;


import idea.model.qnrlibrary.Questionnaires;
import java.awt.BorderLayout;
import java.awt.Label;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.swing.outline.Outline;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Questionnaires Top component which displays a table of questionnaires that
 * meet criteria from the OpeningEditorTopComponent.
 */
@ConvertAsProperties(
        dtd = "-//idea.iquest.explorer//IQuestionnaires//EN" ,
        autostore = false )
@TopComponent.Description(
        preferredID = "IQuestionnairesTopComponent" ,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( position = 1 , mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "idea.iquest.explorer.IQuestionnairesTopComponent" )
@ActionReferences( 
{
    @ActionReference( path = "Menu/Window" , position = 433 ) ,
    //@ActionReference( path = "Menu/Window/SearchResults" , position = 333 ) ,
    @ActionReference( path = "Menu/File/Open" , position = 1300 , separatorBefore = 1290 ) ,
    //@ActionReference( path = "Menu/File/SearchResults" , position = 433 )
} )
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_IQuestionnairesAction" ,
        preferredID = "IQuestionnairesTopComponent" )
@Messages(
        {
    "CTL_IQuestionnairesAction=Questionnaires" ,
    "CTL_IQuestionnairesTopComponent=Questionnaires Window" ,
    "HINT_IQuestionnairesTopComponent=This is a Questionnaires window"
} )
public final class IQuestionnairesTopComponent extends TopComponent implements ExplorerManager.Provider
{

    private static ExplorerManager exMgr = new ExplorerManager();
    
    private OutlineView ov;


    /**
     * xtor.
     */
    public IQuestionnairesTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_IQuestionnairesTopComponent() );
        setToolTipText( Bundle.HINT_IQuestionnairesTopComponent() );

        putClientProperty( "print.printable" , Boolean.TRUE );
        putClientProperty( "print.name" , "Ed's Questionnaires" );

        setLayout( new BorderLayout() );

        // I believe this associates a lookup that goes and gets the Properties for a Prop window update
        associateLookup( ExplorerUtils.createLookup( exMgr , getActionMap() ) );
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        beanTreeView1 = new org.openide.explorer.view.BeanTreeView();
        btnQnrUpdate = new javax.swing.JButton();
        pnlQnrNorth = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        beanTreeView2 = new org.openide.explorer.view.BeanTreeView();
        lblQuestionnaires = new java.awt.Label();

        btnQnrUpdate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(btnQnrUpdate, "Update");
        btnQnrUpdate.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnQnrUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlQnrNorthLayout = new javax.swing.GroupLayout(pnlQnrNorth);
        pnlQnrNorth.setLayout(pnlQnrNorthLayout);
        pnlQnrNorthLayout.setHorizontalGroup(
            pnlQnrNorthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 420, Short.MAX_VALUE)
        );
        pnlQnrNorthLayout.setVerticalGroup(
            pnlQnrNorthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 43, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(beanTreeView2);

        lblQuestionnaires.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        lblQuestionnaires.setText("Questionnaires");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblQuestionnaires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblQuestionnaires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnQnrUpdateActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnQnrUpdateActionPerformed
    {//GEN-HEADEREND:event_btnQnrUpdateActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnQnrUpdateActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView1;
    private org.openide.explorer.view.BeanTreeView beanTreeView2;
    private javax.swing.JButton btnQnrUpdate;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label lblQuestionnaires;
    private javax.swing.JPanel pnlQnrNorth;
    // End of variables declaration//GEN-END:variables


    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }


    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    /**
     *
     * @return the explorer manager for this window
     */
    @Override
    public ExplorerManager getExplorerManager()
    {
        return exMgr;
    }

//    public static InstanceContent getInstanceContent() {
//        return content;
//    }

    /**
     * This is called from the search button in the Opening Editor window
     *
     * @param resultList of Questionnaires to be displayed.
     */
    public void displayQuestionnaires( List<Questionnaires> resultList )
    {
        ExplorerManager exm = getExplorerManager();
        Children kids = Children.create( new IQuestionnairesChildFactory( resultList ) , true );
        Node rootNode = new IQuestionnairesRootNode( kids );
        //OutlineView ov = new OutlineView( "Survey: Ref Period" );
        ov = new OutlineView( "Survey: Ref Period" );

        /**
         * This sets up the column headers and their contents.
         */
        ov.setPropertyColumns(
                //"survey" , "Survey" ,
                //"refPeriod" , "Ref Period" ,
                "formNumber" , "Form Number" ,
                "title" , "Title" ,
                "subTitle" , "Subtitle" ,
                "createdBy" , "Created By" );
        /**
         * This sets up the leftmost column to be the node only. Only it can be
         * double-clicked.
         */
        Outline o = ov.getOutline();
        o.setRootVisible( false );
//        o.getColumnModel().getColumn( 0 );
//        o.getColumnModel().getColumn( 0 ).setMaxWidth( 15 );
//      o.getColumnModel().removeColumn(o.getColumnModel().getColumn( 0 ));

        remove( lblQuestionnaires );
        String qnrLabelText = "Questionnaires -- ";
        int size = resultList.size();
        String res = (size == 1 ? size + " result" : size + " results");
        lblQuestionnaires.setText( qnrLabelText + res);
        add( lblQuestionnaires , BorderLayout.NORTH );
//        add( btnQnrUpdate, BorderLayout.NORTH);
        //add( pnlQnrNorth, BorderLayout.NORTH);
        add( ov , BorderLayout.CENTER );
        exm.setRootContext( rootNode );

    }


    /**
     * 
     * @return 
     */
    public Label getLblQuestionnaires()
    {
        return lblQuestionnaires;
    }

    /**
     * 
     * @return 
     */
    public OutlineView getOv()
    {
        return ov;
    }


    /**
     * 
     * @param lblQuestionnaires 
     */
    public void setLblQuestionnaires( Label lblQuestionnaires )
    {
        this.lblQuestionnaires = lblQuestionnaires;
    }
    
    

}
